<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #t1 tr td{width: 100px;height: 30px;}
        .box1{background: red;}
        .box0{background: blue;}
    </style>
    <script>
        window.onload = function(){
            var oRow = document.getElementById("row");
            var oCol = document.getElementById("col");
            var oBtn = document.getElementById("btn");
            var oT1 = document.getElementById("t1");
            //给表格上的删除按钮添加事件属性
            oT1.onclick = function(ev){
                var e = ev||window.event;
                var target = e.target||window.event.srcElement;
                if(target.nodeName.toLowerCase()=="button"){
                    oT1.removeChild(target.parentNode.parentNode);
                }
            }
            oBtn.onclick = function(){
                if(!oRow.value||!oCol.value){
                    alert("请输入对应的行列，生成表格");
                }else{
                    for(var i =0 ;i<oRow.value;i++){
                        var oTr = document.createElement("tr");
                        for(var j=0;j<oCol.value;j++){
                            var oTd = document.createElement("td");
                            oTr.appendChild(oTd);
                        }
                        //添加一个删除按钮
                        var oClose = document.createElement("td");
                        oClose.innerHTML = "<button>删除</button>";
                        oTr.appendChild(oClose);
                        oTr.className = "box"+i%2;
                        oT1.appendChild(oTr);
                    }
                }
            }
        }
    </script>
</head>
<body>
    <input type="text" placeholder="行" id="row">
    <input type="text" placeholder="列" id="col">
    <button id = "btn">生成</button>
    <table id="t1" border="1"></table>
</body>
</html>